package com.ronan.imweb.utils;

import cn.hutool.json.JSONUtil;
import com.ronan.common.pojo.SendMsgReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * FileName    : com.withufuture.imweb.utils
 * Description : Kafka消息队列数据生产
 *
 * @author : ronan
 * @version : 1.0
 * Create Date : 2021/3/23 16:42
 **/
@Component
public class Producer {

    /** 离线消息 topic */
    private static final String OFFLINE_MESSAGE = "OFFLINE_MESSAGE";
    /** 离线消息前缀+accid */
    private static final String OFFLINE_MESSAGE_PREFIX = "OFFLINE_MESSAGE-";
    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * 离线消息
     *
     * @param msg 消息
     */
    public void offlineMessages(SendMsgReq msg) {
        kafkaTemplate.send(OFFLINE_MESSAGE, OFFLINE_MESSAGE_PREFIX + msg.getTo(), JSONUtil.toJsonStr(msg));
    }
}
